<?php

namespace App\Repository\Statist;

use App\Models\Order\OrderModel as models;
use Illuminate\Support\Facades\Cache;
use App\Repository\RoleRepository;
use Illuminate\Support\Facades\DB;
use App\Repository\BaseRepository;
use App\Repository\Product\ProductRepository;
use App\Repository\UploadRepository;
use App\Repository\Order\OrderInfoRepository;
use App\Repository\Order\OrderLogRepository;
use App\Repository\Order\OrderProductRepository;
use App\Repository\Product\GoodsRepository;
use App\Jobs\OrderSubmit;
use Carbon\Carbon;
use App\Repository\Admin\AdminUsersLogRepository;
use App\Repository\Statist\OrderStatistRepository;
use App\Models\Order\OrderInfoModel as model;
use Monolog\Logger;
use Monolog\Formatter\LineFormatter;
use Monolog\Handler\RotatingFileHandler;
use App\Models\CatchConf\DouYinCatchModel;

class CreateOrderStatistRepository extends  BaseRepository
{
  public function __construct()
  {
    $this->model = new model();
    $this->statist = new OrderStatistRepository();
    $this->orderModel = new models();
    $this->douModel = new DouYinCatchModel();
    $this->log = new Logger('createdorder');
    $this->log->pushHandler(
      (new RotatingFileHandler(storage_path('/logs/createdorder.log'), 14, Logger::DEBUG, true, 0777))->setFormatter(new LineFormatter(null, null, true, true))
    );
    parent::__construct();
  }
  // 查询所有的店铺信息
  public function getAllShop(){
    $adminUser = DB::table('admin_user')->where(['role_id'=>2])->get();
    if(!$adminUser){
      return false;
    }
    $shopArr = [];
    foreach($adminUser as $key => $v){
      $shopArr[] = $v->shop_id;
    }
  }
  // 所有店铺生成统计数据
  public function createStatistOrderAll($time = ''){
    $timeStart = date('Y-m-d 00:00:00', strtotime('-1 day'));
    $timeEnd = date('Y-m-d 23:59:59', strtotime('-1 day'));
    if (!empty($time)) {
      $timeStart = date('Y-m-d 00:00:00', strtotime($time));
      $timeEnd = date('Y-m-d 23:59:59', strtotime($time));
    }
    $whereOrder[] = ['created_at', '>=', $timeStart];
    $whereOrder[] = ['created_at', '<=', $timeEnd];
    
  }
  //生成统计数据  生成单天的下单 发货 激活 数据
  public function createStatistOrder($time = '')
  {
    $timeStart = date('Y-m-d 00:00:00', strtotime('-1 day'));
    $timeEnd = date('Y-m-d 23:59:59', strtotime('-1 day'));
    if (!empty($time)) {
      $timeStart = date('Y-m-d 00:00:00', strtotime($time));
      $timeEnd = date('Y-m-d 23:59:59', strtotime($time));
    }
    // 下单数据
    $whereOrder[] = ['created_at', '>=', $timeStart];
    $whereOrder[] = ['created_at', '<=', $timeEnd];
    $numsOrder =  $this->model->where($whereOrder)->select(DB::raw('count(*) as nums'), 'shop_id')->groupBy('shop_id')->get(); // 下单数据
    if (!empty($numsOrder)) {
      foreach ($numsOrder as $key => $v) {
        $add = [
          'created' => date('Y-m-d', strtotime($timeEnd)),
          'status' => 100,
          'nums' => $v->nums,
          'shop_id' => $v->shop_id,
        ];
        $this->statist->add($add);
      }
    }

    $whereDev[] = ['deliver_at', '>=', $timeStart];
    $whereDev[] = ['deliver_at', '<=', $timeEnd];
    $numsDev = $this->model->where($whereDev)->select(DB::raw('count(*) as nums'), 'shop_id')->groupBy('shop_id')->get(); // 发货数据
    $this->log->info('发货信息的数据' . json_encode($numsDev));
    // $add = [
    //   'created' => date('Y-m-d', strtotime($timeEnd)),
    //   'status' => 101,
    //   'nums' => $numsDev
    // ];
    // $this->statist->add($add);
    if (!empty($numsDev)) {
      foreach ($numsDev as $key => $v) {
        $add = [
          'created' => date('Y-m-d', strtotime($timeEnd)),
          'status' => 101,
          'nums' => $v->nums,
          'shop_id' => $v->shop_id,
        ];
        $this->statist->add($add);
      }
    }

    $whereAct[] = ['activat_at', '>=', $timeStart];
    $whereAct[] = ['activat_at', '<=', $timeEnd];
    $numsAct = $this->model->where($whereAct)->select(DB::raw('count(*) as nums'), 'shop_id')->groupBy('shop_id')->get(); // 激活数据
    $this->log->info('激活信息的数据' . json_encode($numsAct));
    // $add[] = [
    //   'created' => date('Y-m-d', strtotime($timeEnd)),
    //   'status' => 102,
    //   'nums' => $numsAct,
    // ];
    // $this->statist->add($add);
    if (!empty($numsAct)) {
      foreach ($numsAct as $key => $v) {
        $add = [
          'created' => date('Y-m-d', strtotime($timeEnd)),
          'status' => 102,
          'nums' => $v->nums,
          'shop_id' => !empty($v->shop_id) ? $v->shop_id : 5,
        ];
        $this->statist->add($add);
      }
    }
    return true;
  }
  // 生成 产品的单量数据
  public function createStatistProduct($time = '')
  {
    $timeStart = date('Y-m-d 00:00:00', strtotime('-1 day'));
    $timeEnd = date('Y-m-d 23:59:59', strtotime('-1 day'));
    if (!empty($time)) {
      $timeStart = date('Y-m-d 00:00:00', strtotime($time));
      $timeEnd = date('Y-m-d 23:59:59', strtotime($time));
    }

    $whereOrder[] = ['created_at', '>=', $timeStart];
    $whereOrder[] = ['created_at', '<=', $timeEnd];
    $res = $this->orderModel->where($whereOrder)->select('product_name', DB::raw('count(*) as nums'), 'product_id', 'shop_id')->groupBy('product_id', 'product_name', 'shop_id')->get();
    if (!empty($res)) {
      foreach ($res as $key => $v) {
        $add = [
          'created' => date('Y-m-d', strtotime($timeEnd)),
          'product_id' => $v->product_id,
          'product_name' => $v->product_name,
          'nums' => $v->nums,
          'shop_id' => $v->shop_id,
          'status' => 200
        ];
        $this->statist->add($add);
      }
    }
    return $this->returnMsg(200, '已运行');
  }
  //生成代理和产品的单量数据
  public function createStatistProductAndDist_back($page = 1, $time = '')
  {
    // 查询出来 分销商
    $page = 1;
    $nums = 5;
    $skip = ($page - 1) * $nums;
    $res = DB::table('supplier')->skip($skip)->take($nums)->get();
    if (!empty($res)) {
      foreach ($res as $key => $v) {
        $data = [
          'sup_id' => $v->supplier_id,
          'shop_id' => $v->shop_id
        ];
        $this->createStatistProductAndDistOne($data, $time);
      }
    }
    if (empty($res) || count($res) < $nums) {
      $page = 0;
    }
    if ($page <= 0) {
      return $this->returnMsg(200, '代理商和产品的关系运行完');
    }
    // 停留5s 然后接着运行
    return $this->returnMsg(200, '运行第一页' . $page);
  }
  // 代理和商品的单量数据
  public function createStatistProductAndDistOne($time = '')
  {
    $timeStart = date('Y-m-d 00:00:00', strtotime('-1 day'));
    $timeEnd = date('Y-m-d 23:59:59', strtotime('-1 day'));
    if (!empty($time)) {
      $timeStart = date('Y-m-d 00:00:00', strtotime($time));
      $timeEnd = date('Y-m-d 23:59:59', strtotime($time));
    }
    $whereOrder[] = ['created_at', '>=', $timeStart];
    $whereOrder[] = ['created_at', '<=', $timeEnd];
    // sup_shop_id   0,sup_shop_id,shop_id
    // product_id    
    // shop_id
    // $whereOrder[] = ['sup_shop_id', 'like', '%.' . $data['sup_id'] . '.' . $data['shop_id'] . '.%'];
    $res = $this->orderModel->where($whereOrder)->select('goods_name', DB::raw('count(*) as nums'), 'goods_id', 'shop_name', 'shop_id', 'sup_shop_id')->groupBy('goods_id', 'goods_name', 'sup_shop_id', 'shop_id', 'shop_name')->get();
    if (!empty($res)) {
      foreach ($res as $key => $v) {
        if (empty($v->sup_shop_id) || empty($v->shop_id)) {
          continue;
        }
        $add = [
          'created' => date('Y-m-d', strtotime($timeEnd)),
          'goods_id' => $v->goods_id,
          'goods_name' => $v->goods_name,
          'nums' => $v->nums,
          'shop_id' => $v->sup_shop_id,
          'dist_name' => $v->shop_name,
          'dist_id' => $v->shop_id,
          'status' => 300
        ];
        $this->statist->add($add);
      }
    }
    return $this->returnMsg(200, '已运行');
  }
  // 抖店 数据统计
  public function createDouAll (){
    // 店铺区分   总量 提交上游 发货 激活 异常  type = 400
    // 套餐区分   总量 提交上游 发货 激活 异常  type = 500
    // 代理区分   总量 提交上游 发货 激活 异常  type = 600
    // 
    //  一个表: 列 代理(本店)  行 套餐 总量:0/提交上游:0/发货:0/激活:0/异常:0  
    //  一个表: 列 套餐  行 总量 提交上游 发货 激活 异常  order_status goods_id goods_name  time
    //  一个表: 列 来源  行 套餐 总量 提交上游 发货 激活 异常  order_status time source  goods_id goods_name
    //  一个表: 列 
    // 套餐 来源 总量 提交上游 发货 激活 异常 
    // 用户倒序 desc

  }
  // status = 1000 本店 status = 2000 抖店 status = 3000 代理
  /**
   * type  1 订单总量 2 提交数量 3 发货 4 激活 5异常 6 失败 7 取消 8 提交中 9 审核中 
   * time  跑那一天的数据
   * source 来源
   * order_status 订单状态
   * 
   */
  // 63  本店  需要循环
  public function createOurOrderOne ($type=1,$time='',$source = '',$order_status='',$shop_id=''){
    
    $timeStart = date('Y-m-d 00:00:00', strtotime('-1 day'));
    $timeEnd = date('Y-m-d 23:59:59', strtotime('-1 day'));
    if (!empty($time)) {
      $timeStart = date('Y-m-d 00:00:00', strtotime($time));
      $timeEnd = date('Y-m-d 23:59:59', strtotime($time));
    }
    $whereOrder = [];
    $whereOrder[] = ['shop_id','=',$shop_id];
    switch($type){
      case 1:
        // 总量
        $whereOrder[] = ['created_at', '>=', $timeStart];
        $whereOrder[] = ['created_at', '<=', $timeEnd];
        $order_status = '';
        break;
      case 2:
        // 提交第三方时间
        $whereOrder[] = ['three_time_at', '>=', $timeStart];
        $whereOrder[] = ['three_time_at', '<=', $timeEnd];
        $order_status = 1;
        break;
      case 3:
        // 发货
        $whereOrder[] = ['deliver_at', '>=', $timeStart];
        $whereOrder[] = ['deliver_at', '<=', $timeEnd];
        $order_status = 20;
        break;
      case 4:
        // 激活
        $whereOrder[] = ['activat_at', '>=', $timeStart];
        $whereOrder[] = ['activat_at', '<=', $timeEnd];
        $order_status = 40;
        break;
      case 5:
        // 异常
        $whereOrder[] = ['created_at', '>=', $timeStart];
        $whereOrder[] = ['created_at', '<=', $timeEnd];
        $order_status = 50;
        break;
      case 6:
        $whereOrder[] = ['created_at', '>=', $timeStart];
        $whereOrder[] = ['created_at', '<=', $timeEnd];
        $order_status = 60;
        break;
      case 7:
        $whereOrder[] = ['created_at', '>=', $timeStart];
        $whereOrder[] = ['created_at', '<=', $timeEnd];
        $order_status = 51;
        break;
      case 8:
          $whereOrder[] = ['created_at', '>=', $timeStart];
          $whereOrder[] = ['created_at', '<=', $timeEnd];
          $order_status = 0;
          break;
      case 9:
        $whereOrder[] = ['created_at', '>=', $timeStart];
        $whereOrder[] = ['created_at', '<=', $timeEnd];
        $order_status = 10;
        break;
    }
    
    if(isset($source) && $source != ''){
      $whereOrder[] = ['source','=',$source];
    }
    if(isset($order_status) && $order_status != ''){
      $whereOrder[] = ['status','=',$order_status];
    }
    // 统计的数据
    // DB::table('order_info')->where($whereOrder)->count();
    // 查询的是本店的数据  ' order_info  查询的是 订单表  防止数据量太大 给个分页
    // $numsDev = $this->model->where($whereOrder)->select(DB::raw('count(*) as nums'), 'shop_id','goods_id')->groupBy('shop_id','goods_id')->get();
    DB::connection()->enableQueryLog();
    $numsDev = $this->model->where($whereOrder)->count();
    $log = DB::getQueryLog();
    var_dump($log);
    if (!empty($numsDev)) {
      foreach ($numsDev as $key => $v) {
        $add = [
          'created' => date('Y-m-d', strtotime($timeEnd)),
          'status' => 1000,
          'nums' => $v->nums,
          'shop_id' => $v->shop_id,
          'source' => $v->source,
          'order_status' => $v->status,
          'goods_id' => $v->goods_id
        ];
        $this->statist->add($add);
      }
    }
  }
  // 抖店的订单  可以根据 group_by shop_id goods_id source order_status
  public function createOurDouone($type=1,$time='',$source = '',$order_status=''){
    $timeStart = date('Y-m-d 00:00:00', strtotime('-1 day'));
    $timeEnd = date('Y-m-d 23:59:59', strtotime('-1 day'));
    if (!empty($time)) {
      $timeStart = date('Y-m-d 00:00:00', strtotime($time));
      $timeEnd = date('Y-m-d 23:59:59', strtotime($time));
    }
    $whereOrder = [];
    switch($type){
      case 1:
        // 总量
        $whereOrder[] = ['created_at', '>=', $timeStart];
        $whereOrder[] = ['created_at', '<=', $timeEnd];
        $order_status = '';
        break;
      case 2:
        // 提交第三方时间
        $whereOrder[] = ['three_time_at', '>=', $timeStart];
        $whereOrder[] = ['three_time_at', '<=', $timeEnd];
        $order_status = 1;
        break;
      case 3:
        // 发货
        $whereOrder[] = ['deliver_at', '>=', $timeStart];
        $whereOrder[] = ['deliver_at', '<=', $timeEnd];
        $order_status = 20;
        break;
      case 4:
        // 激活
        $whereOrder[] = ['activat_at', '>=', $timeStart];
        $whereOrder[] = ['activat_at', '<=', $timeEnd];
        $order_status = 40;
        break;
      case 5:
        // 异常
        $whereOrder[] = ['created_at', '>=', $timeStart];
        $whereOrder[] = ['created_at', '<=', $timeEnd];
        $order_status = 50;
        break;
      case 6:
        $whereOrder[] = ['created_at', '>=', $timeStart];
        $whereOrder[] = ['created_at', '<=', $timeEnd];
        $order_status = 60;
        break;
      case 7:
        $whereOrder[] = ['created_at', '>=', $timeStart];
        $whereOrder[] = ['created_at', '<=', $timeEnd];
        $order_status = 51;
        break;
      case 8:
          $whereOrder[] = ['created_at', '>=', $timeStart];
          $whereOrder[] = ['created_at', '<=', $timeEnd];
          $order_status = 0;
          break;
      case 9:
        $whereOrder[] = ['created_at', '>=', $timeStart];
        $whereOrder[] = ['created_at', '<=', $timeEnd];
        $order_status = 10;
        break;
    }
    
    if(isset($source) && $source != ''){
      $whereOrder[] = ['source','=',$source];
    }
    if(isset($order_status) && $order_status != ''){
      $whereOrder[] = ['status','=',$order_status];
    }
    // 统计的数据
    // DB::table('order_info')->where($whereOrder)->count();
    // 查询的是本店的数据  ' order_info  查询的是 订单表
    $numsDev = $this->douModel->where($whereOrder)->select(DB::raw('count(*) as nums'), 'shop_id','goods_id','source','status')->groupBy('shop_id','goods_id','source','status')->get();
    if (!empty($numsDev)) {
      foreach ($numsDev as $key => $v) {
        $add = [
          'created' => date('Y-m-d', strtotime($timeEnd)),
          'status' => 2000,
          'nums' => $v->nums,
          'shop_id' => $v->shop_id,
          'source' => $source,
          'order_status' => $order_status
        ];
        $this->statist->add($add);
      }
    }
  }
  // 代理的订单 
  public function createAgentOrderOne ($type=1,$time='',$source = '',$order_status='',$shop_id= ''){
    if(empty($shop_id)){
      return false;
    }
    $timeStart = date('Y-m-d 00:00:00', strtotime('-1 day'));
    $timeEnd = date('Y-m-d 23:59:59', strtotime('-1 day'));
    if (!empty($time)) {
      $timeStart = date('Y-m-d 00:00:00', strtotime($time));
      $timeEnd = date('Y-m-d 23:59:59', strtotime($time));
    }
    $whereOrder = [];
    switch($type){
      case 1:
        // 总量
        $whereOrder[] = ['created_at', '>=', $timeStart];
        $whereOrder[] = ['created_at', '<=', $timeEnd];
        $order_status = '';
        break;
      case 2:
        // 提交第三方时间
        $whereOrder[] = ['three_time_at', '>=', $timeStart];
        $whereOrder[] = ['three_time_at', '<=', $timeEnd];
        $order_status = 1;
        break;
      case 3:
        // 发货
        $whereOrder[] = ['deliver_at', '>=', $timeStart];
        $whereOrder[] = ['deliver_at', '<=', $timeEnd];
        $order_status = 20;
        break;
      case 4:
        // 激活
        $whereOrder[] = ['activat_at', '>=', $timeStart];
        $whereOrder[] = ['activat_at', '<=', $timeEnd];
        $order_status = 40;
        break;
      case 5:
        // 异常
        $whereOrder[] = ['created_at', '>=', $timeStart];
        $whereOrder[] = ['created_at', '<=', $timeEnd];
        $order_status = 50;
        break;
      case 6:
        $whereOrder[] = ['created_at', '>=', $timeStart];
        $whereOrder[] = ['created_at', '<=', $timeEnd];
        $order_status = 60;
        break;
      case 7:
        $whereOrder[] = ['created_at', '>=', $timeStart];
        $whereOrder[] = ['created_at', '<=', $timeEnd];
        $order_status = 51;
        break;
      case 8:
          $whereOrder[] = ['created_at', '>=', $timeStart];
          $whereOrder[] = ['created_at', '<=', $timeEnd];
          $order_status = 0;
          break;
      case 9:
        $whereOrder[] = ['created_at', '>=', $timeStart];
        $whereOrder[] = ['created_at', '<=', $timeEnd];
        $order_status = 10;
        break;
    }
    
    if(isset($source) && $source != ''){
      $whereOrder[] = ['source','=',$source];
    }
    if(isset($order_status) && $order_status != ''){
      $whereOrder[] = ['status','=',$order_status];
    }
    if(!empty($shop_id)){
      $whereOrder[] = ['shop_id_str','like','%.'.$shop_id.'.%'];
    }
    // 统计的数据
    // DB::table('order_info')->where($whereOrder)->count();
    // 查询的是代理的数据  ' order_info  查询的是 订单表  防止数据量太大 给个分页
    $numsDev = $this->douModel->where($whereOrder)->select(DB::raw('count(*) as nums'), 'shop_id','goods_id')->groupBy('shop_id','goods_id')->get();
    if (!empty($numsDev)) {
      foreach ($numsDev as $key => $v) {
        $add = [
          'created' => date('Y-m-d', strtotime($timeEnd)),
          'status' => 3000,
          'nums' => $v->nums,
          'dist_id' => $v->shop_id,
          'source' => $v->source,
          'order_status' => $v->status,
          'shop_id' => $shop_id,
          'goods_id' => $v->goods_id // 商品id 需要读取
        ];
        $this->statist->add($add);
      }
    }
  }
  // 抖店 套餐 来源 类型  group_by shop_id goods_id source order_status
  public function getType(){
    return [
      1 => '订单总量',
      2 => '提交数量',
      3 => '发货',
      4 => '激活',
      5 => '异常',
      6 => '失败',
      7 => '取消',
      8 => '提交中',
      9 => '审核中',
    ];
  }
  public function getSource(){
    return [
      0 => '导入1',
      1 => '抖音',
      2 => '导入',
      3 => 'H5',
      4 => '百度',
      5 => 'Api',
      6 => '供应商抖音',
    ];
  }
}
